package com.win.controller.mysql;

import com.win.service.HappinessService;
import com.win.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;


@RestController
@RequestMapping("upload")
@CrossOrigin
public class HappinessController {
    @Autowired
    private HappinessService happinessService;

    @PostMapping("/{username}")
    public Result importExcelFile(@RequestParam("file") MultipartFile file ,@PathVariable String username) throws IOException {
        //获取前端上传的文件名称
        String uploadFileName = file.getOriginalFilename();
        String fileType = uploadFileName.split("\\.")[1];
        if (fileType.equals("xlsx")) {
            happinessService.importExcelFile(file,username);
        }
        if (fileType.equals("csv")) {
            happinessService.importCSVFile(file,username);
        }
        return Result.ok(null);
    }
}
